// pages/toothExam/toothExam.js
const app=getApp()
Page({
  data: {
    images:["https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/ze-arrow-left%20lmgi6zF@3x.png","https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/tooth4/childback.png"],
    loadedImages:0,
    ifready:true,
    showModalStatus: false,
    currentUser:[],
    showtext:['','',''],
    token:'',
    testurl:'',
    jibing:['龋齿','口腔溃疡','牙齿变色','牙龈炎'],
    toothProblemsList:{"red":'https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/%E7%BA%A2%E6%A1%86.png',"green":'https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/%E7%BB%BF%E6%A1%86.png','red2':'https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/%E7%BA%A2%E6%B3%A1%E6%A1%86.png','green2':'https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/%E7%BB%BF%E6%B3%A1%E6%A1%86.png','bg':'https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/%E7%89%99%E9%BD%BF%E9%97%AE%E9%A2%981%20(1).png','tanhao':'https://gitee.com/ToBugCoding/my-photo-bed/raw/master/imgurl/%E5%85%AC%E5%8F%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%87%8D%E5%86%99/%E7%89%99%E9%BD%BF%E5%8F%B9%E5%8F%B7.png'},
    probability:0.5,
    totalAdvices:["治疗建议一","治疗建议二","治疗建议三","治疗建议四"],
    diseaseName:["龋齿","溃疡","牙齿变色","牙龈炎"]
  },
  back:function(){
    wx.redirectTo({
      url: '/pages/tooth4/tooth4',
    })
  },
  onShow() {
   
    
    
  },
  onLoad(){
    
    
   setTimeout(() => {
     var that=this
    wx.request({
      url: 'https://www.aistomatology.cn/wx/getuserDisase',
      method: 'POST',
      header:{  
        'content-type':'application/x-www-form-urlencoded'
      },
      data: {
            code:app.globalData.usercode
      },
      success:function(savedata){
        var dis = savedata.data.disases
        console.log(dis)
        that.setData({
          currentUser:dis,
         
        })
      },
   })
   }, 500);
   var that=this
    if(that.data.ifready!='none'){
      let images = this.data.images;
      images.forEach((imgSrc, index) => {
        wx.getImageInfo({
          src: imgSrc,
          success: () => {
            // 图片加载成功
            this.setData({
              loadedImages: this.data.loadedImages + 1
            });
            if (this.data.loadedImages === images.length) {
              if(this.data.ifready==true)
              {
                setTimeout(() => {
                  that.setData({
                    ifready:'none'
                  })
                }, 500);
              }
            }
          },
          fail: () => {
            // 图片加载失败
            console.log(`图片${index + 1}加载失败`);
          }
        });
      });
    }
  },
  examine:function(e){
    var message=['','','']
    var advices=[0,0,0,0]
    this.powerDrawer();
    wx.showLoading({
      title: '加载中',
    })
    var that=this
    var p=e.currentTarget.dataset.id
    console.log(p)
    var url=this.data.currentUser[p].imgurl
    var dieas=this.data.currentUser[p].content
    if(dieas==null){
      message[0]="您很健康"
    }else 
    {
      for(var i=0;i<dieas.length;i++)
      {
        advices[dieas[i][5]]++;
      }
    }
    console.log(advices)
    for(var i=0;i<advices.length;++i)
    {
      if(advices[i]!=0)
      {
        message[0]+="发现"+advices[i]+"处"+that.data.diseaseName[i];
      }
    }
    message[0]+="\n"
    if(dieas!=null){
      message[0]+="共"+dieas.length+"处疾病"
    }
  
   
    message[1]=this.data.currentUser[p].data
    var adviceTag=0;
    for(var i=0;i<advices.length;++i)
    {
      if(advices[i]!=0)
      {
        message[2]+=(++adviceTag).toString()+"."+that.data.totalAdvices[i]+"\n";
      }
    }
    this.setData({
      showtext:message                                    
    })
    console.log(dieas)
    const query=wx.createSelectorQuery();
    query.select('#myCanvas').fields({ node: true, size: true }).exec((res)=>{

      const canvas = res[0].node
      const ctx = canvas.getContext('2d')
      canvas.width=640;
      canvas.height=640;
      const scale=1;
      ctx.scale(scale, scale)
      ctx.font =  10/scale + 'px Arial'

      let image = canvas.createImage();//创建iamge实例
      image.src=url
      const num=640;
      image.onload = function () {
      ctx.drawImage(image,0, 0,num,num); // 背景图
      console.log("大图加载成功")
      var tag=['red','green']
      var imgPool = []
      if(dieas==null)
      {
       wx.hideLoading()
        return 0
        
      }
      for(var i=0;i<dieas.length;++i)
      {
        if(dieas[i][4]>that.data.probability){
          imgPool.push({"imgSrc":that.data.toothProblemsList[tag[0]],"x":dieas[i][0],"y":640-dieas[i][3],"width":dieas[i][2]-dieas[i][0],"height":dieas[i][3]-dieas[i][1]})
        }else  imgPool.push({"imgSrc":that.data.toothProblemsList[tag[1]],"x":dieas[i][0],"y":640-dieas[i][3],"width":dieas[i][2]-dieas[i][0],"height":dieas[i][3]-dieas[i][1]})
        
      }
  //多张图像绘制
 var promise_list = [] 
 imgPool.forEach((item)=>{
  promise_list.push(canvas.createImage())
 })
 var imgpos=0;
 var loadimg=0;
 imgPool.forEach((item,index)=>{
  promise_list[loadimg].src=item.imgSrc
  promise_list[loadimg].onload=function(){
      imgpos++;
      if(imgpos==imgPool.length)
      {
        imgpos=0;
       for(imgpos;imgpos<imgPool.length;++imgpos)
       {
        ctx.drawImage(
          promise_list[imgpos],
          imgPool[imgpos].x,
          imgPool[imgpos].y,
          imgPool[imgpos].width,
          imgPool[imgpos].height)
       }
       console.log("小图加载成功")
       wx.hideLoading()
      }
  }
  loadimg++;
 })
     }

    

    })
  },
  powerDrawer: function (e) { 
    var currentStatu = "open";  
    this.util(currentStatu) 
  }, 
  powerDrawer2: function (e) { 
    var currentStatu = "close";  
    this.util(currentStatu) 
  }, 
  util: function(currentStatu){ 
    /* 动画部分 */ 
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({ 
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });  
       
    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;  
   
    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();  
   
    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({ 
      animationData: animation.export() 
    }) 
       
    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () { 
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();  
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({ 
        animationData: animation  
      }) 
         
      //关闭  
      if (currentStatu == "close") { 
        this.setData( 
          { 
            showModalStatus: false 
          } 
        );  
      } 
    }.bind(this), 200) 
     
    // 显示  
    if (currentStatu == "open") { 
      this.setData( 
        { 
          showModalStatus: true 
        } 
      );  
    } 
  } ,
})